//水果成篮 力扣904
//滑动窗口
//用哈希结构（unordered_map）记录已经计算的种类
class Solution {
public:
	int totalFruit(vector<int>& fruits) {
		int left = 0;
		int right = 0;
		int n = fruits.size();
		int ans = 0;
		
		unordered_map<int,int> cnt;
		for( ; right < n; right++)
		{
			cnt[fruits[right]]++;
			while(cnt.size() > 2)
			{
				cnt[fruits[left]]--;
				if(cnt[fruits[left]] == 0) cnt.erase(fruits[left]);
				left++;
				
			}
			ans = max(ans,right - left + 1);
		}
		return ans;
	}
};
